package br.com.salao.ejb.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import br.com.salao.ejb.dao.DiaSemanaDao;
import br.com.salao.entity.DiaSemana;

public class DiaSemanaDaoImpl implements DiaSemanaDao {	

	private EntityManager em;
	private final GenericDao<DiaSemana> dao;
	private Logger logger = Logger.getLogger(DiaSemanaDaoImpl.class.getName());

	public DiaSemanaDaoImpl(EntityManager em) {
		this.em = em;
		dao = new GenericDao<DiaSemana>(em, DiaSemana.class);
	}

	public DiaSemana inserir(DiaSemana diaSemana) {
		return dao.salvar(diaSemana);		
	}

	public List<DiaSemana> listar(){		
		return dao.lista();
	}		

	public DiaSemana update(DiaSemana diaSemana){
		return dao.update(diaSemana);
	}
	
	public DiaSemana buscarDiaSemanaPorIdEstabelecimentos(int idEstabelecimento){

		DiaSemana diaSemana = null;

		try {		
			Query query = em.createQuery("from DiaSemana e where e.estabelecimento.id = :idEstabelecimento");
			query.setParameter("idEstabelecimento", idEstabelecimento);
			diaSemana = (DiaSemana) query.getSingleResult();	
			
		} catch (Exception e) {
			diaSemana = null;			
			logger.error(e.getCause());
			logger.error(e.getMessage());
			e.printStackTrace();
			
		} 
		return diaSemana;			
	}	

}
